import scrapy

# 肉禽类
class MeatSpider(scrapy.Spider):
    name = 'meat'
    allowed_domains = ['meishichina.com']
    start_urls = [
        'https://www.meishichina.com/YuanLiao/category/rql/',
        'https://www.meishichina.com/YuanLiao/category/scl/',
        'https://www.meishichina.com/YuanLiao/category/shucailei/',
        'https://www.meishichina.com/YuanLiao/category/guopinlei/',
        'https://www.meishichina.com/YuanLiao/category/mmdr/',
        'https://www.meishichina.com/YuanLiao/category/tiaoweipinl/',
        'https://www.meishichina.com/YuanLiao/category/yaoshiqita/'
    ]

    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(url=url,callback=self.parse)
        pass

    # 首页
    def parse(self, response):
        div_list = response.xpath('/html/body/div[4]/div/div')
        for div in div_list:
            # 获取食材列表
            li_list = div.xpath('./div/ul/li')
            for li in li_list:
                href = li.xpath('./a/@href').extract_first()
                href = 'https://www.meishichina.com' + href
                # 查询做法
                yield scrapy.Request(url=href,callback=self.parse_type)
        pass

    # 做法列表
    def parse_type(self, response):
        print('--------------当前响应url-------->'+response.url)
        li_list = response.xpath('/html/body/div[5]/div/div[1]/div[2]/ul/li')
        # 获取当前页的做法列表
        for li in li_list:
            href = li.xpath('./a[1]/@href').extract_first()
            # 获取做法详情
            yield scrapy.Request(url=href, callback=self.parse_details)
        # 下一页
        next_page = response.xpath("//*/div[@class='ui-page-inner']/a[last()]/text()").extract_first()
        if '下一页' == next_page:
            next_url = response.xpath("//*/div[@class='ui-page-inner']/a[last()]/@href").extract_first()
            next_url = 'https://www.meishichina.com' + next_url
            print('--------------下一页-------->' + next_url)
            #yield scrapy.Request(url=next_url,callback=self.parse_type)
        pass

    # 做法详情
    def parse_details(self, response):
        # 标题
        title = response.xpath('//*[@id="recipe_title"]/text()').extract_first()
        # 封面
        cover = response.xpath('//*[@id="recipe_De_imgBox"]/a/img/@src').extract_first()
        # 主料
        materials = []
        material_li = response.xpath('//*/div/fieldset[1]/div/ul/li')
        for li in material_li:
            a = li.xpath("./span[@class='category_s1']/a")
            if a:
                category_s1 = a.xpath("./b/text()").extract_first()
                category_s2 = li.xpath("span[@class='category_s2']/text()").extract_first()
            else:
                category_s1 = li.xpath("span[@class='category_s1']/b/text()").extract_first()
                category_s2 = li.xpath("span[@class='category_s2']/text()").extract_first()

            material = {
                'category_s1': category_s1,
                'category_s2': category_s2
            }
            materials.append(material)

        # 辅料
        accessories = []
        accessory_li = response.xpath('//*/div/fieldset[2]/div/ul/li')
        for li in accessory_li:
            a = li.xpath("./span[@class='category_s1']/a")
            if a:
                category_s1 = a.xpath("./b/text()").extract_first()
                category_s2 = li.xpath("span[@class='category_s2']/text()").extract_first()
            else:
                category_s1 = li.xpath("span[@class='category_s1']/b/text()").extract_first()
                category_s2 = li.xpath("span[@class='category_s2']/text()").extract_first()

            accessory = {
                'category_s1': category_s1,
                'category_s2': category_s2
            }
            accessories.append(accessory)

        # 备注
        remarks = []
        remarks_li = response.xpath('/html/body/div[5]/div/div[1]/div[3]/div/div[3]/ul/li')
        for li in remarks_li:
            category_s1 = li.xpath("span[@class='category_s1']/a/text()").extract_first()
            category_s2 = li.xpath("span[@class='category_s2']/text()").extract_first()
            remark = {
                'category_s1': category_s1,
                'category_s2': category_s2
            }
            remarks.append(remark)

        # 做法步骤
        steps = []
        step_li = response.xpath('/html/body/div[5]/div/div[1]/div[3]/div/div[5]/ul/li')
        for li in step_li:
            recipeStep_img = li.xpath('./div[1]/img/@src').extract_first()
            if recipeStep_img is None:
                recipeStep_img = '#'
            recipeStep_num = li.xpath('./div[2]/div/text()').extract_first()
            recipeStep_word = li.xpath('./div[2]/text()').extract_first()
            step = {
                'recipeStep_img': recipeStep_img,
                'recipeStep_num': recipeStep_num,
                'recipeStep_word': recipeStep_word
            }
            steps.append(step)

        h3 = response.xpath("/html/body/div[5]/div/div[1]/div[3]/div/div[6]/h3")
        categorys = []
        if  h3:
            # 小窍门
            recipeTip = response.xpath("/html/body/div[5]/div/div[1]/div[3]/div/div[7]/text()").extract_first().strip()
            # 所属分类
            a_list = response.xpath("/html/body/div[5]/div/div[1]/div[3]/div/div[10]/a")
            for a in a_list:
                category = a.xpath('./text()').extract_first()
                categorys.append(category)
        else:
            # 小窍门
            recipeTip = 'NULL'
            # 所属分类
            a_list = response.xpath("/html/body/div[5]/div/div[1]/div[3]/div/div[8]/a")
            if a_list:
                for a in a_list:
                    category = a.xpath('./text()').extract_first()
                    categorys.append(category)
            else:
                a_list = response.xpath("/html/body/div[5]/div/div[1]/div[3]/div/div[7]/a")
                for a in a_list:
                    category = a.xpath('./text()').extract_first()
                    categorys.append(category)
        yield {
            'title': title,
            'cover': cover,
            'materials': materials,
            'accessories': accessories,
            'remarks': remarks,
            'steps': steps,
            'recipeTip': recipeTip,
            'categorys': categorys
        }
        pass